//
//  AppDelegate.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/16.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "AppDelegate.h"
#import "LSHomeViewController.h"

#import "ZKTransitionTestDemoVC.h"
#import "KZListenSpeakVocabularyImporveVC.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //开启bug监测
    [self setupBugly];
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    KUserDefaults(@"http://172.16.41.235:2048/",@"SystemIP");
    [self enterApp];
    
//    KZListenSpeakVocabularyImporveVC *home = [[KZListenSpeakVocabularyImporveVC alloc] init];
//    ByBaseNaviController *navigationController = [[ByBaseNaviController alloc] initWithRootViewController:home];
//    self.window.rootViewController = navigationController;
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSString *old_userID = KGetUserDefaults(LG_Old_UserID);
    NSString *old_serverUrl = KGetUserDefaults(LG_Old_ServerUrl);
    NSString *addressString = [AddressOperater getBaseAddress];
    NSInteger state = [[[Authentication alloc] init] getUserTokenState];//判断是否在线
    if ([old_serverUrl isEqualToString:addressString]) {
        if (state==1) {//token有效
            if([DR_UserInfo.UserID isEqualToString:old_userID]) {
                if ([self isLoginNavRoot]) {
                    NSLog(@"在登录界面，重新登录。。。");
                    [self setupLoginViewController];
                } else {
                    NSLog(@"恢复活跃。。。");
                }
            } else {
                NSLog(@"切换了账号");
                [self setupLoginViewController];
            }
        } else {
            //token失效，登出
            NSLog(@"token失效");
        }
        
    } else {
        //切换了服务器地址
        NSLog(@"切换了服务器地址");
        [self setupLoginViewController];
    }
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - 进入系统
- (void)enterApp {
    
    NSInteger state = [[[Authentication alloc] init] getUserTokenState];//判断是否在线
    if (state==1) {
        //已登录
        KUserDefaults(@"1", BKIsLogin);
        [self getSystemIP];
        [self getImgIP];
        [self getTestLayerWithInfo:[[[Authentication alloc]init] userInfofromCheyChain]];

        LSHomeViewController *home = [[LSHomeViewController alloc] init];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:home];
        self.window.rootViewController = navigationController;
        
    } else {
        
        KUserDefaults(@"0", BKIsLogin);
        [self setupLoginViewController];
    }
}

#pragma mark - 启动登录界面
- (void)setupLoginViewController {
    Authentication *auth = [[Authentication alloc] init];
    [auth loginWithSystemID:101 titleName:@"智能备考学习" success:^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //存储本次账号和地址
            NSString *addressString = [AddressOperater getBaseAddress];
            KUserDefaults(addressString, LG_Old_ServerUrl);
            NSString *userID = DR_UserInfo.UserID;
            KUserDefaults(userID, LG_Old_UserID);
            //本次登录成功
            KUserDefaults(@"1", BKIsLogin);
            dispatch_async(dispatch_get_main_queue(), ^{
                //在主线程中执行
                [self getSystemIP];
                [self getImgIP];
                [self getTestLayerWithInfo:[[[Authentication alloc]init] userInfofromCheyChain]];
                
                LSHomeViewController *home = [[LSHomeViewController alloc] init];
                UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:home];
                self.window.rootViewController = navigationController;
                
            });
        });
    }];
}


@end
